package com.github.ecbp.user.service.common;

import com.github.ecbp.common.api.IErrorCode;
import com.google.common.base.Optional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public enum UserCodeAndMessageEnum implements IErrorCode {
    //201-299系统模块错误
    USER_NOT_EXISTS_ERROR(1001, "用户不存在"),
    USER_EXISTED_ERROR(1002, "用户已存在"),
    USER_PHONE_EXISTED_ERROR(1003, "手机号已注册"),
    USER_SAVE_ERROR(1007, "用户信息保存失败"),
    USER_CODE_ERROR(1008, "验证失败"),
    USER_ICAPTCHA_CODE_ERROR(1009, "验证码错误"),
    USER_CODE_PARAM_ERROR(1010, "姓名或手机号错误"),
    USER_MSG_OUT_OF_LIMIT_ERROR(1011, "今日找回密码已达上限, 请明天再试"),
    USERNAME_PASSWORD_ERROR(1012, "用户名或密码错误"),
    USERNAME_FORBIDDEN_ERROR(1013, "您的账户已被禁用，请联系管理员后重试"),

    DEPARTMENT_SAVE_ERROR(1019, "部门保存失败"),

    USER_ROLE_NOT_EXISTS_ERROR(1020, "角色信息不存在"),
    USER_ROLE_NAME_ERROR(1021, "角色信息不能为空"),
    USER_ROLE_DUPLICATE(1022, "角色名称重复"),
    USER_ROLE_SAVE_ERROR(1023, "角色信息保存失败"),
    USER_ROLE_DEFAULT_SAVE_ERROR(1024, "当前用户类型已存在默认角色"),
    ROLE_MENU_SAVE_ERROR(1025, "角色菜单关系保存失败"),
    ROLE_PERMISSION_SAVE_ERROR(1026, "角色权限关系保存失败"),
    ROLE_USING_ERROR(1028, "此角色下有用户，请清除所有用户再禁用"),

    MENU_SAVE_ERROR(1060, "菜单信息保存失败"),
    MENU_NOT_EXISTS_ERROR(1061, "菜单信息不存在"),
    PERMISSION_SAVE_ERROR(1070, "权限信息保存失败"),
    PERMISSION_DELETE_ERROR(1071, "权限信息删除失败"),
    PERMISSION_NOT_EXISTS_ERROR(1072, "权限信息不存在"),
    MENU_PERMISSION_SAVE_ERROR(1067, "菜单权限关系保存失败"),

    DELETE_MENU_EXCEPTION(1067, "菜单删除失败"),
    ;

    //code
    private Integer code;

    //描述
    private String message;

    /**
     * 根据code获取状态值信息枚举
     *
     * @param code
     * @return 对应的枚举信息，如果code不存在，则返回null
     * @throws NullPointerException code为空则抛出该异常
     */
    public static UserCodeAndMessageEnum getByCode(Integer code) {
        Optional.of(code);
        List<UserCodeAndMessageEnum> codeAndMessageEnums = Arrays.asList(UserCodeAndMessageEnum.values());
        Map<Integer, UserCodeAndMessageEnum> messageEnumMapByCode =
                codeAndMessageEnums.stream().collect(Collectors.toMap(UserCodeAndMessageEnum::getCode, e -> e));
        return messageEnumMapByCode.get(code);
    }

    UserCodeAndMessageEnum(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    @Override
    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    @Override
    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

}
